package ae.pcfc.cldos.online.common.facade.provider;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import ae.pcfc.cldos.online.common.clddb.domain.AppMetaData;
import ae.pcfc.cldos.online.common.clddb.domain.manager.AppMetaDataDOM;
import ae.pcfc.cldos.online.common.clddb.domain.manager.ApplicationStatusDOM;
import ae.pcfc.cldos.online.common.clddb.domain.manager.ServiceMasterDOM;
import ae.pcfc.cldos.online.common.etksdb.domain.Client;
import ae.pcfc.cldos.online.common.etksdb.domain.User;
import ae.pcfc.cldos.online.common.etksdb.domain.manager.UserDomainObjectManager;
import ae.pcfc.cldos.online.common.facade.BaseFacade;
import ae.pcfc.cldos.online.common.facade.ReportFacade;
import ae.pcfc.cldos.online.common.manager.FNPManager;
import ae.pcfc.cldos.online.common.manager.GSReportManager;
import ae.pcfc.cldos.online.common.tscdb.domain.TTscClientCompany;
import ae.pcfc.cldos.online.common.vo.ReportRequestVO;
import ae.pcfc.cldos.online.common.vo.UserInformationVO;
import ae.pcfc.cldos.online.common.vo.UserVO;
import ae.pcfc.cldos.online.common.vo.report.ReportApplicationVO;
import ae.pcfc.cldos.online.common.vo.tasks.TaskRequestDbRestrictions;
import ae.pcfc.cldos.online.common.vo.tasks.TaskRequestParamsVO;
import ae.pcfc.cldos.online.fnp.service.client.vo.FNPReportResponseVO;
import ae.pcfc.cldos.online.fnp.service.client.vo.FNPTransReportResponseVO;
import ae.pcfc.cldos.online.gs.ConstantsGS;
import ae.pcfc.cldos.online.report.service.vo.EmployeeMasterReportReqVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeMasterReportResponseVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeTransactionMasterReportResponseVO;
import ae.pcfc.cldos.online.report.service.vo.EmployeeTransactionReportReqVO;

/**
 * 
 * @author akhader
 *
 */
public class ReportFacadeImpl extends BaseFacade implements ReportFacade {

	private UserDomainObjectManager userDomainObjectManager;
	private ApplicationStatusDOM applicationStatusDOM;
	private ServiceMasterDOM serviceMasterDOM;
	private AppMetaDataDOM appMetaDataDOM;
	private FNPManager fnpManager;
	private GSReportManager gsReportManager;
	
	
	public GSReportManager getGsReportManager() {
		return gsReportManager;
	}

	public void setGsReportManager(GSReportManager gsReportManager) {
		this.gsReportManager = gsReportManager;
	}

	public ae.pcfc.cldos.online.common.manager.FNPManager getFnpManager() {
		return fnpManager;
	}

	public void setFnpManager(
			ae.pcfc.cldos.online.common.manager.FNPManager fnpManager) {
		this.fnpManager = fnpManager;
	}

	public ApplicationStatusDOM getApplicationStatusDOM() {
		return applicationStatusDOM;
	}

	public void setApplicationStatusDOM(ApplicationStatusDOM applicationStatusDOM) {
		this.applicationStatusDOM = applicationStatusDOM;
	}

	public void setUserDomainObjectManager(
			UserDomainObjectManager userDomainObjectManager) {
		this.userDomainObjectManager = userDomainObjectManager;
	}
	public void setServiceMasterDOM(ServiceMasterDOM serviceMasterDOM) {
		this.serviceMasterDOM = serviceMasterDOM;
	}
	public void setAppMetaDataDOM(AppMetaDataDOM appMetaDataDOM) {
		this.appMetaDataDOM = appMetaDataDOM;
	}


	/* (non-Javadoc)
	 * @see ae.pcfc.cldos.online.common.facade.AdminTasksFacade#getAllAppRequests(ae.pcfc.cldos.online.common.vo.tasks.TaskRequestVO)
	 */
	@Transactional
	public List<ReportApplicationVO> getApplicationList(ReportRequestVO reportRequestVO){
		List<ReportApplicationVO> reportApplicationVOs = new ArrayList<ReportApplicationVO>();
		UserVO userVO = reportRequestVO.getUserVO();
		
		String userName = userVO.getUsername();
		User user = userDomainObjectManager.findUserByUserName(userName);
		Long clientId = user.getClientId();
		UserInformationVO userInformationVO = new UserInformationVO();
		userInformationVO.setClientId(clientId);
		
		TaskRequestParamsVO taskRequestParamsVO = reportRequestVO.getTaskRequestParamsVO();
		TaskRequestDbRestrictions taskRequestDbRestrictions = new TaskRequestDbRestrictions();
		getMapper().map(taskRequestParamsVO, taskRequestDbRestrictions);
		
		
		String referenceNo = taskRequestParamsVO.getReferenceNo();
		taskRequestDbRestrictions.setRefNo(referenceNo);
		taskRequestDbRestrictions.setServiceId(new Long(taskRequestParamsVO.getServiceName()));

		List<AppMetaData> allAppRequests = appMetaDataDOM.getAllApplications(userInformationVO, taskRequestDbRestrictions);
		if(allAppRequests.size()>0){
			for(AppMetaData appMetaData : allAppRequests){
				ReportApplicationVO reportApplicationVO = new ReportApplicationVO();
				reportApplicationVO.setApplicationId(appMetaData.getApplicationId());
				reportApplicationVO.setApplicationDate(appMetaData.getApplicationDate());
				reportApplicationVO.setReferenceNo(appMetaData.getApplicationId().toString());
				reportApplicationVO.setServiceNameEn(appMetaData.getServiceMaster().getServiceName());
				reportApplicationVO.setStatusDesc(appMetaData.getApplicationStatus().getStatusDesc());
				reportApplicationVO.setWfIncidentId(appMetaData.getWfIncidentId());
				reportApplicationVO.setUserId(appMetaData.getUserId());
				reportApplicationVO.setWfInitiatedBy(appMetaData.getWfInitiatedBy());
				reportApplicationVO.setPaymentAdviceNo(appMetaData.getPaymentAdviceNo());
				reportApplicationVOs.add(reportApplicationVO);
			}
		}
		return reportApplicationVOs;
	}
	
	/**
	 * @param userName
	 * @param startDate
	 * @param endDate
	 * @param type
	 * @param actual
	 * @return fnpReportResponseVO
	 */
	@Transactional
	public FNPReportResponseVO generateClientAccountReport(String userName,Date startDate, Date endDate,String type,boolean actual){
		FNPReportResponseVO fnpReportResponseVO = new FNPReportResponseVO();
		fnpReportResponseVO = fnpManager.generateClientAccountReport(userName, startDate, endDate, type, actual);
		return fnpReportResponseVO;
	}
	
	/**
	 * @param employeeMasterReportReqVO
	 * @return
	 */
	public List<EmployeeMasterReportResponseVO> getEmployeeMasterReportData(EmployeeMasterReportReqVO employeeMasterReportReqVO){
		return gsReportManager.getEmployeeMasterReportData(employeeMasterReportReqVO);
	}
	
	/**
	 * @param employeeMasterReportReqVO
	 * @return
	 */
	@Transactional
	public List<EmployeeTransactionMasterReportResponseVO> getEmployeeTransactionReportData(EmployeeTransactionReportReqVO employeeTransactionReportReqVO,String userName){
	/*	List<EmployeeTransactionMasterReportResponseVO> empList = gsReportManager.getEmployeeTransactionReportData(employeeTransactionReportReqVO);
		List<ReportApplicationVO> reportApplicationVOS =  getCLDApplicationList(userName,employeeTransactionReportReqVO);
		for(EmployeeTransactionMasterReportResponseVO emReportResponseVO :empList){
			String transNo = emReportResponseVO.getTransactionNumber();
			emReportResponseVO.setAppliedThrough(ConstantsGS.APPLIED_THRH_OFFLINE);
			for(ReportApplicationVO  reportApplicationVO: reportApplicationVOS){
				if(reportApplicationVO.getClientRefNo()!=null && reportApplicationVO.getClientRefNo().equals(transNo)){
					emReportResponseVO.setAppliedThrough(ConstantsGS.APPLIED_THRH_ONLINE);
					emReportResponseVO.setAdviseId(reportApplicationVO.getPaymentAdviceNo());
					emReportResponseVO.setBgPaymentAdvNo(reportApplicationVO.getBgPaymentAdviceNo());
					break;
				}
			}
		}*/
		//call fnp to get the finance dtls
		List<FNPTransReportResponseVO> financeList = fnpManager.retreiveFNPTransactionReport(userName, employeeTransactionReportReqVO.getTransactionDateFrom(), employeeTransactionReportReqVO.getTransactionDateTo());
		//set service charges
		/*for(EmployeeTransactionMasterReportResponseVO emReportResponseVO :empList){
			for(FNPTransReportResponseVO fnResponseVO :financeList){
				if(fnResponseVO.getPaymentAdviceId().equals(emReportResponseVO.getAdviseId())){
					emReportResponseVO.setServiceCharge(fnResponseVO.getPaymentAdviceAmount().toString());
					emReportResponseVO.setServiceChargeReceiptNo(fnResponseVO.getReceiptNo());
					break;
				}
			}
		}
		//set bg Charges
		for(EmployeeTransactionMasterReportResponseVO emReportResponseVO :empList){
			if(emReportResponseVO.getBgPaymentAdvNo() != null){
				for(FNPTransReportResponseVO fnResponseVO :financeList){
					if(fnResponseVO.getPaymentAdviceId().equals(emReportResponseVO.getBgPaymentAdvNo())){
						emReportResponseVO.setBgCharge(fnResponseVO.getPaymentAdviceAmount().toString());
						emReportResponseVO.setBgChargeReceiptNo(fnResponseVO.getReceiptNo());
						break;
					}
				}
			}
		}
		return empList;*/
		return new ArrayList<EmployeeTransactionMasterReportResponseVO>();
	}
	
	/**
	 * @param userName
	 * @param employeeTransactionReportReqVO
	 * @return
	 */
	private List<ReportApplicationVO> getCLDApplicationList(String userName,EmployeeTransactionReportReqVO employeeTransactionReportReqVO){
		List<ReportApplicationVO> reportApplicationVOs = new ArrayList<ReportApplicationVO>();
		User user = userDomainObjectManager.findUserByUserName(userName);
		Long clientId = user.getClientId();
		UserInformationVO userInformationVO = new UserInformationVO();
		userInformationVO.setClientId(clientId);
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		TaskRequestDbRestrictions taskRequestDbRestrictions = new TaskRequestDbRestrictions();
		
		String referenceNo = employeeTransactionReportReqVO.getTransactionNumber();
		taskRequestDbRestrictions.setRefNo(referenceNo);
		taskRequestDbRestrictions.setDateFrom(employeeTransactionReportReqVO.getTransactionDateFrom().toString());
		taskRequestDbRestrictions.setDateTo(employeeTransactionReportReqVO.getTransactionDateTo().toString());

		List<AppMetaData> allAppRequests = appMetaDataDOM.getAllApplications(userInformationVO, taskRequestDbRestrictions);
		if(allAppRequests.size()>0){
			for(AppMetaData appMetaData : allAppRequests){
				ReportApplicationVO reportApplicationVO = new ReportApplicationVO();
				reportApplicationVO.setApplicationId(appMetaData.getApplicationId());
				reportApplicationVO.setApplicationDate(appMetaData.getApplicationDate());
				reportApplicationVO.setReferenceNo(appMetaData.getApplicationId().toString());
				reportApplicationVO.setServiceNameEn(appMetaData.getServiceMaster().getServiceName());
				reportApplicationVO.setStatusDesc(appMetaData.getApplicationStatus().getStatusDesc());
				reportApplicationVO.setWfIncidentId(appMetaData.getWfIncidentId());
				reportApplicationVO.setUserId(appMetaData.getUserId());
				reportApplicationVO.setWfInitiatedBy(appMetaData.getWfInitiatedBy());
				reportApplicationVO.setPaymentAdviceNo(appMetaData.getPaymentAdviceNo());
				reportApplicationVO.setBgPaymentAdviceNo(appMetaData.getBgPaymentAdviceNo());
				reportApplicationVOs.add(reportApplicationVO);
			}
		}
		return reportApplicationVOs;
	}
	
}
